package com.kangyi.admin.service.Impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kangyi.admin.mapper.AdminUserMapper;
import com.kangyi.admin.mapper.GuestsStayingMapper;
import com.kangyi.admin.mapper.OperationSituationMapper;
import com.kangyi.admin.service.OperationSituationService;
import com.kangyi.constant.MessageConstant;
import com.kangyi.context.BaseContext;
import com.kangyi.dto.ModifyStatusDTO;
import com.kangyi.dto.OperationPageQueryDTO;
import com.kangyi.entity.NursingHome;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.exception.PermissionsException;
import com.kangyi.result.PageResult;
import com.kangyi.result.Result;
import com.kangyi.vo.AdminUserVO;
import com.kangyi.vo.NursingHomeDetailVO;
import com.kangyi.vo.OperationDetailVO;
import com.kangyi.vo.OperationPageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class OperationSituationServiceImpl implements OperationSituationService {

    @Autowired
    private OperationSituationMapper operationSituationMapper;

    @Autowired
    private GuestsStayingMapper guestsStayingMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;

    //调用后检查当前线程id是否有足够的权限进行后续操作
    public void validateUserPermissions() {
        Integer currentId = BaseContext.getCurrentId();
        AdminUserVO adminUserVO = adminUserMapper.selectById(currentId);
        if (!adminUserVO.getPermissions().equals(1)) {
            throw new PermissionsException(MessageConstant.INSUFFICIENT_PRIVILEGE);
        }
    }

    @Override
    public PageResult pageQuery(OperationPageQueryDTO operationPageQueryDTO) {
        validateUserPermissions();
        PageHelper.startPage(operationPageQueryDTO.getPage(), operationPageQueryDTO.getPageSize());
        Page<OperationPageQueryVO> page = operationSituationMapper.pageQuery(operationPageQueryDTO);
        if (CollUtil.isEmpty(page.getResult())) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        long total = page.getTotal();
        List<OperationPageQueryVO> result = page.getResult();
        for (OperationPageQueryVO vo : result) {
            Integer id = vo.getId();
            vo.setTotalGuest(guestsStayingMapper.countByNursingHomeId(id));
        }
        return new PageResult(total,result);
    }

    @Override
    public OperationDetailVO detailOperation(Integer nursingHomeId) {
        validateUserPermissions();
        OperationDetailVO operationDetailVO = operationSituationMapper.detailOperation(nursingHomeId);
        if (operationDetailVO == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        return operationDetailVO;
    }

    @Override
    public void modifyStatus(ModifyStatusDTO modifyStatusDTO) {
        validateUserPermissions();
        NursingHome build = NursingHome.builder()
                .id(modifyStatusDTO.getNursingHomeId())
                .status(modifyStatusDTO.getStatus())
                .build();
        operationSituationMapper.modifyStatus(build);
    }


}
